# -*- coding: utf-8 -*-
import re

import scrapy

from ChineseLottery.items import WinOrLoseItem, PointItem, HalfItem, BasketballWinOrLose, BasketballPoint, TotalBallItem


class ChinaLotterySpider(scrapy.Spider):
    name = 'china_lottery'
    allowed_domains = ['zgzcw.com']
    start_urls = ['http://www.zgzcw.com/']

    def parse(self, response):
        football_next_type_list = response.xpath('//dl[@class="zcw-nav-dl"]/dd/a/@href').extract()
        print(football_next_type_list)
        # 足球胜负彩
        yield scrapy.Request(football_next_type_list[0], callback=self.parse_sfc)
        # 足球比分
        yield scrapy.Request(football_next_type_list[1], callback=self.parse_point)
        # 足球半全场
        yield scrapy.Request(football_next_type_list[4], callback=self.parse_half)
        yield scrapy.Request(football_next_type_list[3], callback=self.parse_total_ball)
        yield scrapy.Request(football_next_type_list[8], callback=self.parse_basketball_win)
        yield scrapy.Request(football_next_type_list[11], callback=self.parse_basketball_point)

    # 足球胜负彩
    def parse_sfc(self, response):
        rows = response.xpath('//table[@class="mb"]/tbody/tr[contains(@class,"beginBet")]')
        for row in rows:
            win_or_lose = WinOrLoseItem()
            win_or_lose['date'] = row.xpath('@t').extract_first()
            weekdays = row.xpath('td[1]//text()').extract()
            weekday = ''
            for weekday_item in weekdays:
                weekday +=weekday_item
            weekday = re.sub(r'\s+', '', weekday)
            win_or_lose['weekday'] = weekday
            win_or_lose['type'] = row.xpath('td[2]/@title').extract_first()
            win_or_lose['home'] = row.xpath('td[4]/a/text()').extract_first()
            win_or_lose['away'] = row.xpath('td[6]/a/text()').extract_first()
            no_let_list = row.xpath('td[7]/div[@class="tz-area frq"]/a/text()').extract()
            try:
                win_or_lose['no_let_win'] = no_let_list[0]
                win_or_lose['no_let_even'] = no_let_list[1]
                win_or_lose['no_let_lose'] = no_let_list[2]
            except IndexError:
                print("未开售")
            let_list = row.xpath('td[7]/div[@class="tz-area tz-area-2 rqq"]/a/text()').extract()
            try:
                win_or_lose['let_win'] = let_list[0]
                win_or_lose['let_even'] = let_list[1]
                win_or_lose['let_lose'] = let_list[2]
                win_or_lose['let_ball'] = row.xpath('td[7]/div[@class="tz-area tz-area-2 rqq"]/em/text()').extract_first()
            except IndexError:
                print("未开售")
            yield win_or_lose

    # 足球比分
    def parse_point(self, response):
        rows = response.xpath('//table[@class="mb"]/tbody/tr[contains(@class,"beginBet")]')
        for row in rows:
            point = PointItem()
            point['date'] = row.xpath('@t').extract_first()
            weekdays = row.xpath('td[1]//text()').extract()
            weekday = ''
            for weekday_item in weekdays:
                weekday += weekday_item
            weekday = re.sub(r'\s+', '', weekday)
            point['weekday'] = weekday
            point['type'] = row.xpath('td[2]/@title').extract_first()
            point['home'] = row.xpath('td[4]/a/text()').extract_first()
            point['away'] = row.xpath('td[6]/a/text()').extract_first()
            point_string = row.xpath('input/@value').extract_first()
            if point_string != None:
                point_string = point_string.split(' ')
                point['one_zero'] = point_string[0]
                point['two_zero'] = point_string[1]
                point['two_one'] = point_string[2]
                point['three_zero'] = point_string[3]
                point['three_one'] = point_string[4]
                point['three_two'] = point_string[5]
                point['four_zero'] = point_string[6]
                point['four_one'] = point_string[7]
                point['four_two'] = point_string[8]
                point['five_zero'] = point_string[9]
                point['five_one'] = point_string[10]
                point['five_two'] = point_string[11]
                point['win_other'] = point_string[12]
                point['zero_zero'] = point_string[13]
                point['one_one'] = point_string[14]
                point['two_two'] = point_string[15]
                point['three_three'] = point_string[16]
                point['even_other'] = point_string[17]
                point['zero_one'] = point_string[18]
                point['zero_two'] = point_string[19]
                point['one_two'] = point_string[20]
                point['zero_three'] = point_string[21]
                point['one_three'] = point_string[22]
                point['two_three'] = point_string[23]
                point['zero_four'] = point_string[24]
                point['one_four'] = point_string[25]
                point['two_four'] = point_string[26]
                point['zero_five'] = point_string[27]
                point['one_five'] = point_string[28]
                point['two_five'] = point_string[29]
                point['lose_other'] = point_string[30]
                yield point


    # 足球半全场
    def parse_half(self, response):
        rows = response.xpath('//table[@class="mb"]/tbody/tr[contains(@class,"beginBet")]')
        for row in rows:
            half = HalfItem()
            half['date'] = row.xpath('@t').extract_first()
            weekdays = row.xpath('td[1]//text()').extract()
            weekday = ''
            for weekday_item in weekdays:
                weekday += weekday_item
            weekday = re.sub(r'\s+', '', weekday)
            half['weekday'] = weekday
            half['type'] = row.xpath('td[2]/@title').extract_first()
            half['home'] = row.xpath('td[4]/a/text()').extract_first()
            half['away'] = row.xpath('td[6]/a/text()').extract_first()
            half_list = row.xpath('td[7]/div[@class="bqc-area"]/a/text()').extract()
            half['win_win'] = half_list[0]
            half['win_even'] = half_list[1]
            half['win_lose'] = half_list[2]
            half['even_win'] = half_list[3]
            half['even_even'] = half_list[4]
            half['even_lose'] = half_list[5]
            half['lose_win'] = half_list[6]
            half['lose_even'] = half_list[7]
            half['lose_lose'] = half_list[8]
            yield half

    # 足球总比分
    def parse_total_ball(self, response):
        rows = response.xpath('//table[@class="mb"]/tbody/tr[contains(@class,"beginBet")]')
        for row in rows:
            total_ball = TotalBallItem()
            total_ball['date'] = row.xpath('@t').extract_first()
            weekdays = row.xpath('td[1]//text()').extract()
            weekday = ''
            for weekday_item in weekdays:
                weekday += weekday_item
            weekday = re.sub(r'\s+', '', weekday)
            total_ball['weekday'] = weekday
            total_ball['type'] = row.xpath('td[2]/@title').extract_first()
            total_ball['home'] = row.xpath('td[4]/a/text()').extract_first()
            total_ball['away'] = row.xpath('td[6]/a/text()').extract_first()
            total_ball_list = row.xpath('td[7]/div[@class="zjq-area"]/a/text()').extract()
            total_ball['zero'] = total_ball_list[0]
            total_ball['one'] = total_ball_list[1]
            total_ball['two'] = total_ball_list[2]
            total_ball['three'] = total_ball_list[3]
            total_ball['four'] = total_ball_list[4]
            total_ball['five'] = total_ball_list[5]
            total_ball['six'] = total_ball_list[6]
            total_ball['seven'] = total_ball_list[7]
            yield total_ball

    # 篮球胜负
    def parse_basketball_win(self, response):
        rows = response.xpath('//table[@class="mb"]/tbody/tr')
        for row in rows:
            win_or_lose = BasketballWinOrLose()
            win_or_lose['date'] = row.xpath('td[3]/span[3]/@title').extract_first()
            weekdays = row.xpath('td[1]//text()').extract()
            weekday = ''
            for weekday_item in weekdays:
                weekday += weekday_item
            weekday = re.sub(r'\s+', '', weekday)
            win_or_lose['weekday'] = weekday
            win_or_lose['type'] = row.xpath('td[2]/span[@class="g_qt"]/text()').extract_first()
            win_or_lose['home'] = row.xpath('td[4]/a/text()').extract_first()
            win_or_lose['away'] = row.xpath('td[6]/a/text()').extract_first()
            no_let_list = row.xpath('td[7]/div[@class="bets-area frq"]/a/text()').extract()
            win_or_lose['no_let_win'] = no_let_list[0]
            win_or_lose['no_let_lose'] = no_let_list[1]
            let_list = row.xpath('td[7]/div[@class="bets-area bets-area-2 rqq"]/a/text()').extract()
            win_or_lose['let_win'] = let_list[0]
            win_or_lose['let_lose'] = let_list[1]
            win_or_lose['let_ball'] = row.xpath('td[7]/div[@class="bets-area bets-area-2 rqq"]/em/text()').extract_first()
            yield win_or_lose

    # 篮球大小分
    def parse_basketball_point(self, response):
        rows = response.xpath('//table[@class="mb"]/tbody/tr[contains(@class,"beginBet")]')
        for row in rows:
            point = BasketballPoint()
            point['date'] = row.xpath('td[3]/span[3]/@title').extract_first()
            weekdays = row.xpath('td[1]//text()').extract()
            weekday = ''
            for weekday_item in weekdays:
                weekday += weekday_item
            weekday = re.sub(r'\s+', '', weekday)
            point['weekday'] = weekday
            point['type'] = row.xpath('td[2]/span/text()').extract_first()
            point['home'] = row.xpath('td[4]/a/text()').extract_first()
            point['away'] = row.xpath('td[6]/a/text()').extract_first()
            point_list = row.xpath('td[7]/div[@class="bets-area"]/a/text()').extract()
            point['big'] = point_list[0]
            point['small'] = point_list[1]
            point_raw = row.xpath('td[7]/div[@class="bets-area"]/em/text()').extract_first()
            point['point'] = re.sub(r'\s+', '', point_raw)
            yield point